﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace HRM.DTO
{
    public class Thuvien
    {
        #region " Variable" 
        private SqlConnection _conn;
        //private SqlDataAdapter adapter = new SqlDataAdapter();
        private string _error = "";
        #endregion

        #region " Properties" //---------------------------------
        public SqlConnection Conn
        {
            get { return _conn; }
            set { _conn = value; }
        }
        public string Error
        {
            get { return _error; }
        }
        #endregion

        #region " Methods connect" //------------------------------
        public Thuvien()
        {
            string strconn = @"Data Source=TIEPBA-FTS\SQLEXPRESS;Initial Catalog = HRM;Integrated Security=True";
            _conn = new SqlConnection(strconn);
            
        }

        public bool Connect()
        {
            try
            {
                _conn.Open();
            }
            catch (Exception ex)
            {
                _error = "Không mở kết nối tới CSDL đc - " + ex.ToString();
                return false;
            }
            return true;
        }
        public bool Disconect()
        {
            try
            {
                _conn.Close();
            }
            catch (Exception ex)
            {
                _error = "Không đóng kết nối tới CSDL đc - " + ex.ToString();
                return false;
            }
            return true;
        }
        #endregion
        #region " method Update "
        public bool UpdateTable(DataTable dt)
        {
            if (Conn.State == ConnectionState.Closed)
                if (!Connect())
                {
                    _error = Error;
                    return false;
                }
            int result = -1;
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter();
                result = adapter.Update(dt);
                //result = adapter.Fill(Dataset);
            }
            catch (Exception ex)
            {
                _error = "TV loi Khong load du lieu ra bang dc " + "\n" + ex.ToString();
            }
            if (Disconect())
            {
                _error = Error;
                return false;
            }
            if (result <= 0)
            {
                _error = " Thu vien Khong co du lieu trong bang so ke qua = " + result.ToString();
                return false;
            }
            return true;
        }
        public bool UpdateDataset(DataTable ds)
        {
            if (Conn.State == ConnectionState.Closed)
                if (!Connect())
                {
                    _error = Error;
                    return false;
                }
            int result = -1;
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter();
                result = adapter.Update(ds);
                //result = adapter.Fill(Dataset);
            }
            catch (Exception ex)
            {
                _error = " Loi Khong load du lieu ra bang dc " + "\n" + ex.ToString();
            }
            if (Disconect())
            {
                _error = Error;
                return false;
            }
            if (result <= 0)
            {
                _error = " Thu vien Khong co du lieu trong bang so ke qua = " + result.ToString();
                return false;
            }
            return true;
        }
        #endregion
        #region " method control"

        public void NumOnly(Object sender, KeyPressEventArgs e)
        {
            if (!(char.IsControl(e.KeyChar) || char.IsDigit(e.KeyChar)))
            {
                e.Handled = true;
            }
        }
        public string CurrencyInput(TextBox str)
        {
            TextBox txt = str;
            int a = txt.Text.Length - txt.SelectionStart;
            string OriginalStr = string.Empty;
            OriginalStr = txt.Text.Replace(",", "");

            if (OriginalStr == string.Empty || OriginalStr.Length == 0)
            {
                return txt.Text = "";
            }
            else
            {
                OriginalStr.Trim();
            }
            string NewStr = string.Empty;
            string[] arr = new string[(OriginalStr.Length + 2) / 3];
            if (OriginalStr.Length > 3)
            {
                for (int i = 1; i <= (OriginalStr.Length / 3); i++)
                {
                    arr[i - 1] = OriginalStr.Substring((OriginalStr.Length - 3 * i), 3);
                }
                if (OriginalStr.Length % 3 != 0)
                {
                    arr[(OriginalStr.Length - 1) / 3] = OriginalStr.Substring(0, OriginalStr.Length % 3);
                }
                for (int i = arr.Length - 1; i > 0; i--)
                {
                    NewStr += arr[i] + ",";
                }
                NewStr += arr[0];
                txt.Text = NewStr;
            }
            else
            {
                txt.Text = OriginalStr;
            }
            txt.SelectionStart = txt.Text.Length - a;
            return txt.Text;
        }
        public string CorrectFullName(string str)
        {
            string txt = str;
            txt = txt.Trim();
            txt = txt.ToLower();
            while (txt.IndexOf("  ") > 0)
            {
                txt = txt.Replace("  ", " ");
            }
            int i = txt.IndexOf(" ");
            while (i > 0)
            {
                string temp = txt.Substring(i + 1, 1);
                txt = txt.Remove(i + 1, 1);
                txt = txt.Insert(i + 1, temp.ToUpper());
                i = txt.IndexOf(" ", i + 1);
            }
            string chr = txt.Substring(0, 1);
            txt = txt.Remove(0, 1);
            txt = txt.Insert(0, chr.ToUpper());
            return txt;
        }
        public int Tinhtuoi(DateTime BirthDate)
        {
            return DateTime.Today.Year - BirthDate.Year;
        }

        #endregion
    }
}
